package com.client.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.color.CMMException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;

import com.client.ClientMain;
import com.client.DrawArea;
import com.client.GUIMonitor;
import com.client.listeners.CreateButtonListener;
import com.client.listeners.DoodleListener;
import com.client.listeners.JoinButtonListener;

@SuppressWarnings("serial")
public class DoodleGuiTestMain extends JPanel implements ActionListener {

	private JPanel					northPanel;
	private JPanel					westPanel;
	private JPanel					centerPanel;
	private JPanel					eastPanel;
	private JList					roomList;
	private CreateButtonListener	cbList;
	private JoinButtonListener		jbList;
	private DefaultListModel		listModel;
	private DrawArea				da;
	@SuppressWarnings("unused")
	private GUIMonitor				guim;
	private JPanel					buttonPanel, textPanel;

	public DoodleGuiTestMain(GUIMonitor guim, DrawArea da, DoodleListener d)
	{
		this.guim = guim;
		this.da = da;
		setMinimumSize(new Dimension(1200, 600));
		setPreferredSize(new Dimension(1200, 700));

		// Initializing startpage panels
		northPanel = new JPanel();
		northPanel.setOpaque(false);
		westPanel = new JPanel();
		westPanel.setOpaque(false);
		centerPanel = new JPanel();
		centerPanel.setOpaque(false);
		eastPanel = new JPanel();
		eastPanel.setOpaque(false);
		for (Component c : getComponents())
		{
			c.addKeyListener(d);
		}
		// Adding panels to frame
		// BackgroundPanel bp = new BackgroundPanel(new ImageIcon("imgs/BackgroundLightGray.jpg").getImage());
		setLayout(new BorderLayout());
		setBackground(Color.white);
		add(northPanel, BorderLayout.NORTH);
		add(westPanel, BorderLayout.WEST);
		add(centerPanel, BorderLayout.CENTER);
		add(eastPanel, BorderLayout.EAST);
		centerPanel.addKeyListener(d);

		// Adding headertext to northPanel
		Font headerFont = new Font("MV Boli", Font.BOLD, 48);
		TextComponent headerText = new TextComponent("DOODLING", headerFont, Color.black, 290, 60);
		northPanel.add(headerText);

		// Creating list with open rooms
		System.out.println("f�rs�ker h�mta roomlist");
		Map<Integer, String> rooms = guim.getRoomList();
		System.out.println("roomlist h�mtad");
		listModel = new DefaultListModel();
		for (int i = 0; i < rooms.size(); i++)
		{
			listModel.addElement(rooms.get(i));
		}

		// Object[] testData = { "Chris room", "Davids room", "Thomas room", "Meyers room", "Example room 1",
		// "Example room 2", "Example room 3" };
		roomList = new JList(listModel);
		roomList.setBorder(new RoundedBorder());
		roomList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		roomList.setLayoutOrientation(JList.VERTICAL);
		roomList.setVisibleRowCount(-1);
		roomList.setAlignmentX(CENTER_ALIGNMENT);
		roomList.setFixedCellHeight(25);
		JScrollPane listScroller = new JScrollPane(roomList);
		listScroller.setPreferredSize(new Dimension(250, 300));
		listScroller.setBorder(new RoundedBorder());

		// Creating label to open rooms list in westPanel
		Font labelFont = new Font("Calibri", Font.BOLD, 18);
		// TextComponent roomsLabel = new TextComponent("Open Rooms", labelFont, Color.black, 60, 20);
		JLabel roomsLabel = new JLabel("Open Rooms");
		roomsLabel.setOpaque(false);
		roomsLabel.setFont(labelFont);
		roomsLabel.setAlignmentX(CENTER_ALIGNMENT);

		// Adding content to westPanel
		westPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.PAGE_AXIS));
		westPanel.add(roomsLabel);
		westPanel.add(Box.createRigidArea(new Dimension(0, 7)));
		westPanel.add(listScroller);
		westPanel.add(Box.createRigidArea(new Dimension(0, 90)));

		// Creating buttons to join and create server
		JButton joinRoomButton = new JButton(new ImageIcon("imgs/Button_join_new.png"));
		setButtonSettings(joinRoomButton, 215, 70);
		joinRoomButton.setRolloverIcon(new ImageIcon("imgs/Button_join_new_rollover.png"));
		joinRoomButton.setPressedIcon(new ImageIcon("imgs/Button_join_new_clicked.png"));
		JPanel buttonJoinAndShadow = new JPanel();
		buttonJoinAndShadow.setLayout(new BoxLayout(buttonJoinAndShadow, BoxLayout.PAGE_AXIS));
		buttonJoinAndShadow.add(joinRoomButton);
		ImagePanel button1Shadow = new ImagePanel(new ImageIcon("imgs/Button_shadow.png").getImage());
		jbList = new JoinButtonListener(this, roomList, guim);
		joinRoomButton.addActionListener(jbList);
		buttonJoinAndShadow.add(button1Shadow);

		JButton createRoomButton = new JButton(new ImageIcon("imgs/Button_create_new.png"));
		setButtonSettings(createRoomButton, 215, 70);
		createRoomButton.setRolloverIcon(new ImageIcon("imgs/Button_create_new_rollover.png"));
		createRoomButton.setPressedIcon(new ImageIcon("imgs/Button_create_new_clicked.png"));
		JPanel buttonCreateAndShadow = new JPanel();
		buttonCreateAndShadow.setLayout(new BoxLayout(buttonCreateAndShadow, BoxLayout.PAGE_AXIS));
		buttonCreateAndShadow.add(createRoomButton);
		ImagePanel button2Shadow = new ImagePanel(new ImageIcon("imgs/Button_shadow.png").getImage());
		cbList = new CreateButtonListener(roomList, listModel, guim);
		createRoomButton.addActionListener(cbList);
		buttonCreateAndShadow.add(button2Shadow);

		// Create textareas describing the buttons
		Font helpTextFont = new Font("Calibri", Font.PLAIN, 18);
		JTextArea joinRoomText = new JTextArea("Click on a room and click \"Join\" to join an existing room.");
		setTextAreaSettings(joinRoomText, helpTextFont, 200, 70);

		JTextArea createRoomText = new JTextArea("Click \"Create\" to create a new room.");
		setTextAreaSettings(createRoomText, helpTextFont, 200, 70);

		// Creating panel for buttons
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
		buttonPanel.setOpaque(false);
		buttonPanel.add(Box.createRigidArea(new Dimension(50, 0)));
		buttonPanel.add(buttonJoinAndShadow);
		buttonPanel.add(Box.createRigidArea(new Dimension(100, 0)));
		buttonPanel.add(buttonCreateAndShadow);
		buttonPanel.addKeyListener(d);

		// Creating panel for text
		textPanel = new JPanel();
		textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.LINE_AXIS));
		textPanel.add(Box.createRigidArea(new Dimension(70, 0)));
		textPanel.setOpaque(false);
		textPanel.add(joinRoomText);
		textPanel.add(Box.createRigidArea(new Dimension(120, 0)));
		textPanel.add(createRoomText);

		// Adding content to centerPanel
		centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
		centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 10));
		centerPanel.add(Box.createRigidArea(new Dimension(0, 200)));
		centerPanel.add(buttonPanel);
		// centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
		centerPanel.add(textPanel);

		// Adding content to eastPanel
		eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.PAGE_AXIS));
		eastPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
		eastPanel.add(Box.createRigidArea(new Dimension(230, 10)));

		// Must have this
		setVisible(true);
		hideCreateAndJoin();

		int q = guim.getQuestion();
		if (q == GUIMonitor.JOIN_OR_CREATE_ACTION)
		{
			showCreateAndJoin();
		}
	}

	public void enterDrawingState()
	{
		System.out.println("Enter drawingstate...");
		removeAll();
		add(da);
		validate();
		repaint();
	}

	public void hideCreateAndJoin()
	{
		buttonPanel.setVisible(false);
		textPanel.setVisible(false);
	}

	public void showCreateAndJoin()
	{
		System.out.println("Visar join and create...");
		buttonPanel.setVisible(true);
		textPanel.setVisible(true);
	}

	public void setButtonSettings(JButton button, int width, int height)
	{
		button.setBorderPainted(false);
		button.setRolloverEnabled(true);
		button.setPreferredSize(new Dimension(width, height));
		button.setMaximumSize(new Dimension(width, height));
		button.setAlignmentX(CENTER_ALIGNMENT);
		button.setAlignmentY(CENTER_ALIGNMENT);
	}

	public void setTextAreaSettings(JTextArea area, Font font, int width, int height)
	{
		area.setFont(font);
		area.setLineWrap(true);
		area.setWrapStyleWord(true);
		area.setEditable(false);
		area.setPreferredSize(new Dimension(width, height));
		area.setMaximumSize(new Dimension(width, height));
	}

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		// TODO Auto-generated method stub

	}
	/*
	 * public static void main(String[] arguments) throws InterruptedException { DoodleGuiTestMain frame = new
	 * DoodleGuiTestMain(); frame.pack(); frame.setVisible(true); }
	 */

}
